package com.hyy.lotterysystem.controller;

import com.hyy.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.hyy.lotterysystem.common.exception.ControllerException;
import com.hyy.lotterysystem.common.pojo.CommonResult;
import com.hyy.lotterysystem.common.utils.JacksonUtil;
import com.hyy.lotterysystem.controller.param.CreatePrizeParam;
import com.hyy.lotterysystem.controller.param.PageParam;
import com.hyy.lotterysystem.controller.result.FindPrizeListResult;
import com.hyy.lotterysystem.service.PictureService;
import com.hyy.lotterysystem.service.PriceService;
import com.hyy.lotterysystem.service.dto.PageListDTO;
import com.hyy.lotterysystem.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@Slf4j
@RestController
public class PrizeController {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private PriceService priceService;

    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file) {
        log.info("开始上传文件");
        return pictureService.savePicture(file);
    }

    /*
    * 创建奖品
    * */
//    RequestParm用于简单的数据类型，可以设置默认值 也可以设置可选，
//    ReqestPart用于复杂文件的传输（包括表单数据）对吗——表单就是multipart/form - data
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(
            @Validated @RequestPart("param") CreatePrizeParam param,
            @RequestPart("prizePic") MultipartFile picFile){
            log.info("createParam：{}", JacksonUtil.writeValueAsString(param));

          return CommonResult.success(priceService.createPrize(param, picFile));
    }

    /*
    * 查询奖品列表
    * */
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param){
        log.info("findPrizeList PageParam {}",JacksonUtil.writeValueAsString(param));
       PageListDTO<PrizeDTO> pageListDTO= priceService.findPrizeList(param);
       return CommonResult.success(convertToFindPrizeListResult(pageListDTO));

    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if(pageListDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getRecords()
                        .stream()
                        .map(prizeDTO->{
                                FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                                prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                                prizeInfo.setPrizeName(prizeDTO.getName());
                                prizeInfo.setDescription(prizeDTO.getDescription());
                                prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                                prizeInfo.setPrice(prizeDTO.getPrice());
                                return prizeInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }
}
